package com.shirley.pers.zhufengfmvi.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shirley.pers.zhufengfmvi.Constants;
import com.shirley.pers.zhufengfmvi.R;
import com.shirley.pers.zhufengfmvi.entities.DiscoveryColumnsList;
import com.shirley.pers.zhufengfmvi.entities.DiscoveryColumnsListInfo;
import com.shirley.pers.zhufengfmvi.entities.DiscoveryRecommendItem;
import com.shirley.pers.zhufengfmvi.entities.HighQualityListenList;
import com.shirley.pers.zhufengfmvi.entities.HighQualityListenListInfo;
import com.shirley.pers.zhufengfmvi.entities.RecommendAlbumInfo;
import com.shirley.pers.zhufengfmvi.entities.RecommendAlbums;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 发现模块，推荐栏目适配器<br/>
 * <p/>
 * Copyright (c) , 2016, Shirley Xian <br/>
 * This program is protected by copyright laws. <br/>
 * ClassName: RecommendAdapter<br/>
 * Date: 2016/4/6 16:18
 *
 * @author Shirley Xian
 * @version 1.0
 */
public class RecommendAdapter extends BaseAdapter {

    private Context mContext;
    private List<DiscoveryRecommendItem> mItems;
    private View.OnClickListener mOnClickListener;

    /**
     * 供其它类调用
     *
     * @param onClickListener
     */
    public void setOnClickListener(View.OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public RecommendAdapter(Context context, List<DiscoveryRecommendItem> datas) {
        mContext = context;
        mItems = datas;
    }

    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        int count = 0;
        if (mItems != null) {
            count = mItems.size();
        }
        return count;
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return DiscoveryRecommendItem.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position) {
        //当Item需要创建或者需要复用的时候，ListView自动调用这个方法，来检查，缓冲区中是否有相同类型的View.
        //如果有，才能够复用，如果没有，那么创建新的布局，返回的数值 应该是0-getViewTypeCount()-1
        //因为内部采用类型数值作为缓存区的数组下表
        DiscoveryRecommendItem discoveryRecommendItem = mItems.get(position);
        return discoveryRecommendItem.getItemViewType();
    }

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        int itemViewType = mItems.get(position).getItemViewType();
        switch (itemViewType) {
            case DiscoveryRecommendItem.TYPE_FOCUS_IMAGE:
                break;
            case DiscoveryRecommendItem.TYPE_EDITE_HOT_RECOMMEND:
                //小编推荐  热门推荐
                ret = bindEditHotView(position, convertView, parent);
                break;
            case DiscoveryRecommendItem.TYPE_DISCOVERY_COLUMNS:
                //发现新奇
                ret = bindDiscoveryColumnsView(position, convertView, parent);
                break;
            case DiscoveryRecommendItem.TYPE_SPECIAL_COMLUMNS:
                //精品听单
                ret = bindSpecialColumnView(position, convertView, parent);
                break;
        }
        return ret;
    }

    private View bindDiscoveryColumnsView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(mContext).inflate(R.layout.item_discovery_columns_recommend_discovery_list, parent, false);
        }
        DiscoveryColumnsViewHolder discoveryColumnsViewHolder = (DiscoveryColumnsViewHolder) ret.getTag();
        if (discoveryColumnsViewHolder == null) {
            discoveryColumnsViewHolder = new DiscoveryColumnsViewHolder(ret);
            ret.setTag(discoveryColumnsViewHolder);
        }
        //设置数据
        DiscoveryRecommendItem discoveryRecommendItem = mItems.get(position);
        if (discoveryRecommendItem.isHasMore()) {
            discoveryColumnsViewHolder.item_hasMore_recommend_discovery_list.setVisibility(View.VISIBLE);
        }
        discoveryColumnsViewHolder.item_maintitle_recommend_discovery_list.setText(discoveryRecommendItem.getTitle());
        DiscoveryColumnsList discoveryColumnsList = (DiscoveryColumnsList) discoveryRecommendItem;
        List<DiscoveryColumnsListInfo> list = discoveryColumnsList.getList();
        for (int i = 0; i < list.size(); i++) {
            DiscoveryColumnsListInfo discoveryColumnsListInfo = list.get(i);
            switch (i) {
                case 0:
                    discoveryColumnsViewHolder.item_discovery_columns_title1_recommend_discovery_list.setText(discoveryColumnsListInfo.getTitle());
                    discoveryColumnsViewHolder.item_discovery_columns_subtitle1_recommend_discovery_list.setText(discoveryColumnsListInfo.getSubtitle());
                    String imgUrl1 = discoveryColumnsListInfo.getCoverPath();
                    Picasso.with(mContext).load(imgUrl1).into(discoveryColumnsViewHolder.item_discovery_columns_image1_recommend_discovery_list);
                    break;

                case 1:
                    discoveryColumnsViewHolder.item_discovery_columns_title2_recommend_discovery_list.setText(discoveryColumnsListInfo.getTitle());
                    discoveryColumnsViewHolder.item_discovery_columns_subtitle2_recommend_discovery_list.setText(discoveryColumnsListInfo.getSubtitle());
                    String imgUrl2 = discoveryColumnsListInfo.getCoverPath();
                    Picasso.with(mContext).load(imgUrl2).into(discoveryColumnsViewHolder.item_discovery_columns_image2_recommend_discovery_list);
                    break;
                case 2:
                    discoveryColumnsViewHolder.item_discovery_columns_title3_recommend_discovery_list.setText(discoveryColumnsListInfo.getTitle());
                    discoveryColumnsViewHolder.item_discovery_columns_subtitle3_recommend_discovery_list.setText(discoveryColumnsListInfo.getSubtitle());
                    String imgUrl3 = discoveryColumnsListInfo.getCoverPath();
                    Picasso.with(mContext).load(imgUrl3).into(discoveryColumnsViewHolder.item_discovery_columns_image3_recommend_discovery_list);
                    break;
                case 3:
                    discoveryColumnsViewHolder.item_discovery_columns_title4_recommend_discovery_list.setText(discoveryColumnsListInfo.getTitle());
                    discoveryColumnsViewHolder.item_discovery_columns_subtitle4_recommend_discovery_list.setText(discoveryColumnsListInfo.getSubtitle());
                    String imgUrl4 = discoveryColumnsListInfo.getCoverPath();
                    Picasso.with(mContext).load(imgUrl4).into(discoveryColumnsViewHolder.item_discovery_columns_image4_recommend_discovery_list);
                    break;
            }
        }
        //设置点击事件
        discoveryColumnsViewHolder.item_discovery_columns_image1_recommend_discovery_list.setOnClickListener(mOnClickListener);
        discoveryColumnsViewHolder.item_discovery_columns_image2_recommend_discovery_list.setOnClickListener(mOnClickListener);
        discoveryColumnsViewHolder.item_discovery_columns_image3_recommend_discovery_list.setOnClickListener(mOnClickListener);
        discoveryColumnsViewHolder.item_discovery_columns_image4_recommend_discovery_list.setOnClickListener(mOnClickListener);
        return ret;
    }

    class DiscoveryColumnsViewHolder {
        TextView item_maintitle_recommend_discovery_list;
        View item_hasMore_recommend_discovery_list;

        ImageView item_discovery_columns_image1_recommend_discovery_list;
        TextView item_discovery_columns_title1_recommend_discovery_list, item_discovery_columns_subtitle1_recommend_discovery_list;

        ImageView item_discovery_columns_image2_recommend_discovery_list;
        TextView item_discovery_columns_title2_recommend_discovery_list, item_discovery_columns_subtitle2_recommend_discovery_list;

        ImageView item_discovery_columns_image3_recommend_discovery_list;
        TextView item_discovery_columns_title3_recommend_discovery_list, item_discovery_columns_subtitle3_recommend_discovery_list;

        ImageView item_discovery_columns_image4_recommend_discovery_list;
        TextView item_discovery_columns_title4_recommend_discovery_list, item_discovery_columns_subtitle4_recommend_discovery_list;

        public DiscoveryColumnsViewHolder(View itemView) {
            item_maintitle_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_maintitle_recommend_discovery_list);
            item_hasMore_recommend_discovery_list = itemView.findViewById(R.id.item_hasMore_recommend_discovery_list);
            item_discovery_columns_image1_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_discovery_columns_image1_recommend_discovery_list);
            item_discovery_columns_title1_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_title1_recommend_discovery_list);
            item_discovery_columns_subtitle1_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_subtitle1_recommend_discovery_list);

            item_discovery_columns_image2_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_discovery_columns_image2_recommend_discovery_list);
            item_discovery_columns_title2_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_title2_recommend_discovery_list);
            item_discovery_columns_subtitle2_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_subtitle2_recommend_discovery_list);

            item_discovery_columns_image3_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_discovery_columns_image3_recommend_discovery_list);
            item_discovery_columns_title3_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_title3_recommend_discovery_list);
            item_discovery_columns_subtitle3_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_subtitle3_recommend_discovery_list);

            item_discovery_columns_image4_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_discovery_columns_image4_recommend_discovery_list);
            item_discovery_columns_title4_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_title4_recommend_discovery_list);
            item_discovery_columns_subtitle4_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_discovery_columns_subtitle4_recommend_discovery_list);
        }
    }

    @NonNull
    private View bindSpecialColumnView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(mContext).inflate(R.layout.item_special_columns_recommend_discovery_list, parent, false);
        }
        SpecialColumnsViewHolder specialColumnsViewHolder = (SpecialColumnsViewHolder) ret.getTag();
        if (specialColumnsViewHolder == null) {
            specialColumnsViewHolder = new SpecialColumnsViewHolder(ret);
            ret.setTag(specialColumnsViewHolder);
        }
        //设置数据
        DiscoveryRecommendItem discoveryRecommendItem = mItems.get(position);
        HighQualityListenList highQualityListenList = (HighQualityListenList) discoveryRecommendItem;
        List<HighQualityListenListInfo> list = highQualityListenList.getList();

        if (discoveryRecommendItem.isHasMore()) {
            specialColumnsViewHolder.item_hasMore_recommend_discovery_list.setVisibility(View.VISIBLE);
        }
        specialColumnsViewHolder.item_maintitle_recommend_discovery_list.setText(discoveryRecommendItem.getTitle());

        for (int i = 0; i < list.size(); i++) {
            HighQualityListenListInfo highQualityListenListInfo = list.get(i);
            switch (i) {
                case 0:
                    specialColumnsViewHolder.item_special_columns_title_top.setText(highQualityListenListInfo.getTitle());
                    specialColumnsViewHolder.item_special_columns_subtitle_top.setText(highQualityListenListInfo.getSubtitle());
                    specialColumnsViewHolder.item_special_columns_footnote_top.setText(highQualityListenListInfo.getFootnote());
                    String imageUrlTop = highQualityListenListInfo.getCoverPath();
                    if (imageUrlTop != null) {
                        Picasso.with(mContext).load(imageUrlTop).into(specialColumnsViewHolder.item_special_columns_image_top);
                    }
                    break;
                case 1:
                    specialColumnsViewHolder.item_special_columns_title_bottom.setText(highQualityListenListInfo.getTitle());
                    specialColumnsViewHolder.item_special_columns_subtitle_bottom.setText(highQualityListenListInfo.getSubtitle());
                    specialColumnsViewHolder.item_special_columns_footnote_bottom.setText(highQualityListenListInfo.getFootnote());
                    String imageUrlBottom = highQualityListenListInfo.getCoverPath();
                    if (imageUrlBottom != null) {
                        Picasso.with(mContext).load(imageUrlBottom).into(specialColumnsViewHolder.item_special_columns_image_bottom);
                    }
                    break;
            }
        }
        //绑定事件监听
        specialColumnsViewHolder.item_special_columns_image_top.setOnClickListener(mOnClickListener);
        specialColumnsViewHolder.item_special_columns_image_bottom.setOnClickListener(mOnClickListener);
        return ret;
    }

    class SpecialColumnsViewHolder {
        TextView item_maintitle_recommend_discovery_list;
        View item_hasMore_recommend_discovery_list;
        ImageView item_special_columns_image_top;
        TextView item_special_columns_title_top, item_special_columns_subtitle_top, item_special_columns_footnote_top;

        ImageView item_special_columns_image_bottom;
        TextView item_special_columns_title_bottom, item_special_columns_subtitle_bottom, item_special_columns_footnote_bottom;

        public SpecialColumnsViewHolder(View itemView) {
            item_maintitle_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_maintitle_recommend_discovery_list);
            item_hasMore_recommend_discovery_list = itemView.findViewById(R.id.item_hasMore_recommend_discovery_list);

            item_special_columns_image_top = (ImageView) itemView.findViewById(R.id.item_special_columns_image_top);
            item_special_columns_title_top = (TextView) itemView.findViewById(R.id.item_special_columns_title_top);
            item_special_columns_subtitle_top = (TextView) itemView.findViewById(R.id.item_special_columns_subtitle_top);
            item_special_columns_footnote_top = (TextView) itemView.findViewById(R.id.item_special_columns_footnote_top);

            item_special_columns_image_bottom = (ImageView) itemView.findViewById(R.id.item_special_columns_image_bottom);
            item_special_columns_title_bottom = (TextView) itemView.findViewById(R.id.item_special_columns_title_bottom);
            item_special_columns_subtitle_bottom = (TextView) itemView.findViewById(R.id.item_special_columns_subtitle_bottom);
            item_special_columns_footnote_bottom = (TextView) itemView.findViewById(R.id.item_special_columns_footnote_bottom);
        }
    }

    @NonNull
    private View bindEditHotView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(mContext).inflate(R.layout.item_edit_hot_recommend_discovery_recommend_fragment, parent, false);
        }
        EditorHotViewHolder editorHotViewHolder = (EditorHotViewHolder) ret.getTag();
        if (editorHotViewHolder == null) {
            editorHotViewHolder = new EditorHotViewHolder(ret);
            ret.setTag(editorHotViewHolder);
        }
        //设置数据
        DiscoveryRecommendItem discoveryRecommendItem = mItems.get(position);
        RecommendAlbums albums = (RecommendAlbums) discoveryRecommendItem;
        List<RecommendAlbumInfo> recommendAlbumInfos = albums.getAlbums();
        editorHotViewHolder.item_maintitle_recommend_discovery_list.setText(discoveryRecommendItem.getTitle());
        if (discoveryRecommendItem.isHasMore()) {
            editorHotViewHolder.item_hasMore_recommend_discovery_list.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < recommendAlbumInfos.size(); i++) {
            Map<String,Long> tagMap;
            RecommendAlbumInfo albumInfo = recommendAlbumInfos.get(i);
            switch (i) {
                case 0:
                    editorHotViewHolder.item_left_trackTitle_recommend_discovery_list.setText(albumInfo.getTrackTitle());
                    editorHotViewHolder.item_left_title_recommend_discovery_list.setText(albumInfo.getTitle());
                    String imageUrlLeft = albumInfo.getCoverLarge();
                    Picasso.with(mContext).load(imageUrlLeft).into(editorHotViewHolder.item_left_coverLarge_recommend_discovery_list);

                    tagMap = new HashMap<>();
                    tagMap.put(Constants.EXTRA_ALBUM_ID,albumInfo.getAlbumId());
                    tagMap.put(Constants.EXTRA_TRACK_ID,albumInfo.getTrackId());
                    editorHotViewHolder.item_left_coverLarge_recommend_discovery_list.setTag(tagMap);

                    break;
                case 1:
                    editorHotViewHolder.item_middle_trackTitle_recommend_discovery_list.setText(albumInfo.getTrackTitle());
                    editorHotViewHolder.item_middle_title_recommend_discovery_list.setText(albumInfo.getTitle());
                    String imageUrlMiddle = albumInfo.getCoverLarge();
                    Picasso.with(mContext).load(imageUrlMiddle).into(editorHotViewHolder.item_middle_coverLarge_recommend_discovery_list);

                    tagMap = new HashMap<>();
                    tagMap.put(Constants.EXTRA_ALBUM_ID, albumInfo.getAlbumId());
                    tagMap.put(Constants.EXTRA_TRACK_ID, albumInfo.getTrackId());
                    editorHotViewHolder.item_middle_coverLarge_recommend_discovery_list.setTag(tagMap);

                    break;
                case 2:
                    editorHotViewHolder.item_right_trackTitle_recommend_discovery_list.setText(albumInfo.getTrackTitle());
                    editorHotViewHolder.item_right_title_recommend_discovery_list.setText(albumInfo.getTitle());
                    String imageUrlRight = albumInfo.getCoverLarge();
                    Picasso.with(mContext).load(imageUrlRight).into(editorHotViewHolder.item_right_coverLarge_recommend_discovery_list);

                    tagMap = new HashMap<>();
                    tagMap.put(Constants.EXTRA_ALBUM_ID, albumInfo.getAlbumId());
                    tagMap.put(Constants.EXTRA_TRACK_ID, albumInfo.getTrackId());
                    editorHotViewHolder.item_right_coverLarge_recommend_discovery_list.setTag(tagMap);

                    break;
            }
        }
        //设置控件监听
        editorHotViewHolder.item_left_coverLarge_recommend_discovery_list.setOnClickListener(mOnClickListener);
        editorHotViewHolder.item_middle_coverLarge_recommend_discovery_list.setOnClickListener(mOnClickListener);
        editorHotViewHolder.item_right_coverLarge_recommend_discovery_list.setOnClickListener(mOnClickListener);
        return ret;
    }

    class EditorHotViewHolder {
        TextView item_maintitle_recommend_discovery_list;
        View item_hasMore_recommend_discovery_list;

        ImageView item_left_coverLarge_recommend_discovery_list;
        TextView item_left_title_recommend_discovery_list, item_left_trackTitle_recommend_discovery_list;

        ImageView item_middle_coverLarge_recommend_discovery_list;
        TextView item_middle_title_recommend_discovery_list, item_middle_trackTitle_recommend_discovery_list;

        ImageView item_right_coverLarge_recommend_discovery_list;
        TextView item_right_title_recommend_discovery_list, item_right_trackTitle_recommend_discovery_list;

        public EditorHotViewHolder(View itemView) {
            item_maintitle_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_maintitle_recommend_discovery_list);
            item_hasMore_recommend_discovery_list = itemView.findViewById(R.id.item_hasMore_recommend_discovery_list);
            item_left_coverLarge_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_left_coverLarge_recommend_discovery_list);
            item_left_title_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_left_title_recommend_discovery_list);
            item_left_trackTitle_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_left_trackTitle_recommend_discovery_list);

            item_middle_coverLarge_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_middle_coverLarge_recommend_discovery_list);
            item_middle_title_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_middle_title_recommend_discovery_list);
            item_middle_trackTitle_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_middle_trackTitle_recommend_discovery_list);

            item_right_coverLarge_recommend_discovery_list = (ImageView) itemView.findViewById(R.id.item_right_coverLarge_recommend_discovery_list);
            item_right_title_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_right_title_recommend_discovery_list);
            item_right_trackTitle_recommend_discovery_list = (TextView) itemView.findViewById(R.id.item_right_trackTitle_recommend_discovery_list);
        }
    }
}
